home *** CD-ROM | disk | FTP | other *** search
- /*
- File: CAS_AppleEvent.c
-
- Contains: Code to install, dispatch, and execute Apple event handlers.
-
- Written by: David H Nelson
-
- Copyright © 1993-1995 ComponentWorks, All rights reserved.
-
- Change History (most recent first):
-
- <2> 9/13/95 TWB Comment out unnecessary include of CALib.
- <1> 11/20/93 DHN Created.
-
- */
-
- #include <AppleEvents.h>
- #include <AERegistry.h>
-
- #include "CAS_AppleEvent.h"
- #include "CAS_Globals.h"
-
- #include "CAS_App.h"
- #include "CAS_Doc.h"
- #include "CAS_Win.h"
- #include "CAS_Error.h"
-
- //----------------------------------------------------------------------
- // routine descriptors for our event handlers.
-
- AEEventHandlerUPP gOpenApplicationHandlerUPP = nil;
- AEEventHandlerUPP gOpenDocumentsHandlerUPP = nil;
- AEEventHandlerUPP gPrintDocumentsHandlerUPP = nil;
- AEEventHandlerUPP gQuitApplicationHandlerUPP = nil;
- AEEventHandlerUPP gDummyHandlerUPP = nil;
-
- //----------------------------------------------------------------------
- // local prototypes
-
- #if defined(__cplusplus)
- extern "C"
- {
- #endif
-
- static OSErr AE_GotRequiredParams(
- AppleEvent *theAppleEvent );
- pascal OSErr AE_OpenApplicationHandler(
- AppleEvent *theAppleEvent,
- AppleEvent *reply,
- long refCon );
- pascal OSErr AE_OpenDocumentsHandler(
- AppleEvent *theAppleEvent,
- AppleEvent *reply,
- long refCon );
- pascal OSErr AE_PrintDocumentsHandler(
- AppleEvent *theAppleEvent,
- AppleEvent *reply,
- long refCon );
- pascal OSErr AE_QuitApplicationHandler(
- AppleEvent *theAppleEvent,
- AppleEvent *reply,
- long refCon );
- pascal OSErr AE_DummyHandler(
- AppleEvent *theAppleEvent,
- AppleEvent *reply,
- long refCon );
-
- #if defined(__cplusplus)
- }
- #endif
-
- OSErr GetDescFSSpec(AEDesc *desc, FSSpec* spec);
-
-
- //---------------------------------------------------------------------------
- static OSErr AE_GotRequiredParams(
- AppleEvent *theAppleEvent )
- {
- DescType returnedType;
- Size actualSize;
- OSErr theErr;
-
- theErr =
- AEGetAttributePtr(
- theAppleEvent, keyMissedKeywordAttr,
- typeWildCard, &returnedType,
- nil, 0, &actualSize );
-
- // we got all the required parameters
- if (theErr == errAEDescNotFound)
- theErr = noErr;
- // we missed a required parameter
- else if (theErr == noErr)
- theErr = errAEParamMissed;
- // otherwise the call to AEGetAttributePtr failed
-
- return theErr;
- }
-
- //---------------------------------------------------------------------------
- // AE_OpenApplicationHandler - called once at launch if there are NO documents to open.
- // Do any setup such as creating a new empty document.
- // $$$$$ should we be checking GotRequiredParams here?
-
- pascal OSErr AE_OpenApplicationHandler(
- AppleEvent *theAppleEvent,
- AppleEvent *reply,
- long refCon )
- {
- App_NewMenu();
-
- return noErr;
- }
-
- //---------------------------------------------------------------------------
- // AE_OpenApplicationHandler - called once at launch if there are documents to open.
- // Open the documents that are passed.
-
- pascal OSErr AE_OpenDocumentsHandler(
- AppleEvent *theAppleEvent,
- AppleEvent *reply,
- long refCon )
- {
- AEDescList docList;
- AEKeyword keyword;
- DescType returnedType;
- FSSpec theFSSpec;
- Size actualSize;
- OSErr theErr;
- long index;
- long itemCount;
- AEDesc documentDesc;
-
- // get the doc list.
- theErr = AEGetParamDesc( theAppleEvent, keyDirectObject, typeAEList, &docList );
- if (theErr != noErr)
- return theErr;
-
- // check for missing required parameters
- theErr = AE_GotRequiredParams( theAppleEvent );
- if (theErr != noErr)
- {
- // an error occurred: do the necessary error handling
- AEDisposeDesc( &docList );
- return theErr;
- }
-
- // count the number of descriptor records in the list
- theErr = AECountItems( &docList, &itemCount );
- if (theErr != noErr)
- return theErr;
-
- for (index=1; index<=itemCount; index++)
- {
-
- theErr = AEGetNthDesc(&docList, index, typeWildCard,
- &keyword, &documentDesc);
-
- if (theErr)
- return theErr;
-
- theErr = GetDescFSSpec(&documentDesc, &theFSSpec);
-
- if (theErr)
- return theErr;
-
- if (theErr != noErr)
- DEBUGSTR( "\pAE_OpenDocumentsHandler: error from AEGetNthPtr" );
-
- // open the file
- theErr = Doc_Open( &theFSSpec );
- if (theErr != noErr)
- Error_ShowMessage( kCASMsgWrongFileVersion );
- }
-
- // get rid of the AEDescList
- theErr = AEDisposeDesc( &docList );
- if (theErr != noErr)
- return theErr;
-
- return theErr;
- }
-
- //---------------------------------------------------------------------------
- // Print the documents that are passed.
-
- pascal OSErr AE_PrintDocumentsHandler(
- AppleEvent *theAppleEvent,
- AppleEvent *reply,
- long refCon )
- {
- AEDescList docList;
- AEKeyword keyword;
- DescType returnedType;
- FSSpec theFSSpec;
- Size actualSize;
- OSErr theErr;
- long index;
- long itemCount;
-
- // get the doc list.
- theErr = AEGetParamDesc( theAppleEvent, keyDirectObject, typeAEList, &docList );
- if (theErr != noErr)
- return theErr;
-
- // check for missing required parameters
- theErr = AE_GotRequiredParams( theAppleEvent );
- if (theErr != noErr)
- {
- // an error occurred: do the necessary error handling
- AEDisposeDesc( &docList );
- return theErr;
- }
-
- // count the number of descriptor records in the list
- theErr = AECountItems( &docList, &itemCount );
- if (theErr != noErr)
- DEBUGSTR( "\pPrintDocumentsHandler: error from AECountItems" );
-
- for (index=1; index<=itemCount; index++)
- {
- WindowPtr theWindow;
-
- // get the ith file in doc list.
- theErr =
- AEGetNthPtr(
- &docList, index, typeFSS, &keyword, &returnedType,
- (Ptr)&theFSSpec, (long)sizeof(FSSpec), &actualSize );
- if (theErr != noErr)
- DEBUGSTR( "\pPrintDocumentsHandler: error from AEGetNthPtr");
-
- // open the file
- theErr = Doc_Open( &theFSSpec );
- if (theErr != noErr)
- DEBUGSTR( "\pPrintDocumentsHandler: error from Doc_Open" );
-
- theWindow = App_GetFrontDocWindow();
- if (theWindow != nil)
- {
- // print it and close it
- App_PrintMenu( theWindow );
- Win_Close( theWindow, NULL );
- }
- }
-
- // get rid of the AEDescList
- theErr = AEDisposeDesc( &docList );
- if (theErr != noErr)
- DEBUGSTR("\pPrintDocumentsHandler: error from AEDisposeDesc" );
-
- return noErr;
- }
-
- //---------------------------------------------------------------------------
- // Do any cleanup before quiting and set the gExitFlag
- // $$$$$ This routine should get the Save Options and decide whether to save
- // all docs, don't save, or ask the user.
- pascal OSErr AE_QuitApplicationHandler(
- AppleEvent *theAppleEvent,
- AppleEvent *reply,
- long refCon )
- {
- gExitFlag = App_CloseAll();
- return noErr;
- }
-
- //---------------------------------------------------------------------------
- // Place holder handler that does nothing but return errAEEventNotHandled
-
- pascal OSErr AE_DummyHandler(
- AppleEvent *theAppleEvent,
- AppleEvent *reply,
- long refCon )
- {
- return errAEEventNotHandled;
- }
-
- //---------------------------------------------------------------------------
-
- void AE_InitAppleEvents( void )
- {
- OSErr err;
-
- // Create the routine descriptors and install the handlers for the Required Suite
- gOpenApplicationHandlerUPP = NewAEEventHandlerProc( AE_OpenApplicationHandler );
- err = AEInstallEventHandler( kCoreEventClass, kAEOpenApplication, gOpenApplicationHandlerUPP, 0, false );
-
- if (err) DebugStr ("\p AEInstallEventHandler error");
-
- gOpenDocumentsHandlerUPP = NewAEEventHandlerProc( AE_OpenDocumentsHandler );
- err = AEInstallEventHandler( kCoreEventClass, kAEOpenDocuments, gOpenDocumentsHandlerUPP, 0, false );
-
- if (err) DebugStr ("\p AEInstallEventHandler error");
-
- gPrintDocumentsHandlerUPP = NewAEEventHandlerProc( AE_PrintDocumentsHandler );
- err = AEInstallEventHandler( kCoreEventClass, kAEPrintDocuments, gPrintDocumentsHandlerUPP, 0, false );
-
- if (err) DebugStr ("\p AEInstallEventHandler error");
-
- gQuitApplicationHandlerUPP = NewAEEventHandlerProc( AE_QuitApplicationHandler );
- err = AEInstallEventHandler( kCoreEventClass, kAEQuitApplication, gQuitApplicationHandlerUPP, 0, false );
-
- if (err) DebugStr ("\p AEInstallEventHandler error");
-
- #if 0
- // $$$$$ each of these handlers needs a different type of routine descriptor.
- // Create the routine descriptors and install the handlers for the Core Suite
- gDummyHandlerUPP = NewAEEventHandlerProc( OpenApplicationHandler );
- AEInstallEventHandler( kAECoreSuite, kAEClone, gDummyHandlerUPP, 0, false );
- AEInstallEventHandler( kAECoreSuite, kAEClose, gDummyHandlerUPP, 0, false );
- AEInstallEventHandler( kAECoreSuite, kAECountElements, gDummyHandlerUPP, 0, false );
- AEInstallEventHandler( kAECoreSuite, kAECreateElement, gDummyHandlerUPP, 0, false );
- AEInstallEventHandler( kAECoreSuite, kAEDelete, gDummyHandlerUPP, 0, false );
- AEInstallEventHandler( kAECoreSuite, kAEDoObjectsExist, gDummyHandlerUPP, 0, false );
- AEInstallEventHandler( kAECoreSuite, kAEGetData, gDummyHandlerUPP, 0, false );
- AEInstallEventHandler( kAECoreSuite, kAEGetDataSize, gDummyHandlerUPP, 0, false );
- AEInstallEventHandler( kAECoreSuite, kAEGetClassInfo, gDummyHandlerUPP, 0, false );
- AEInstallEventHandler( kAECoreSuite, kAEGetEventInfo, gDummyHandlerUPP, 0, false );
- AEInstallEventHandler( kAECoreSuite, kAEMove, gDummyHandlerUPP, 0, false );
- AEInstallEventHandler( kAECoreSuite, kAESave, gDummyHandlerUPP, 0, false );
- AEInstallEventHandler( kAECoreSuite, kAESetData, gDummyHandlerUPP, 0, false );
- #endif
- }
-
- //---------------------------------------------------------------------------
-
- void AE_DisposeAppleEvents( void )
- {
- DisposeRoutineDescriptor( gOpenApplicationHandlerUPP );
- DisposeRoutineDescriptor( gOpenDocumentsHandlerUPP );
- DisposeRoutineDescriptor( gPrintDocumentsHandlerUPP );
- DisposeRoutineDescriptor( gQuitApplicationHandlerUPP );
- // DisposeRoutineDescriptor( gDummyHandlerUPP );
- }
-
- //---------------------------------------------------------------------------
-
- void AE_DoAppleEvent(
- EventRecord *theEvent )
- {
- AEProcessAppleEvent( theEvent );
- }
-
-
-
- //==============================================================================
- // GetDescFSSpec
- //==============================================================================
-
- OSErr
- GetDescFSSpec(AEDesc *desc, FSSpec* spec)
- {
- AEDesc resultDesc;
- OSErr err = AECoerceDesc(desc, typeFSS, &resultDesc);
- if (err) return err;
- *spec = **(FSSpec**)(resultDesc.dataHandle);
- AEDisposeDesc(&resultDesc);
- return noErr;
- }
-